﻿using System;
using System.ComponentModel;
using System.IO.IsolatedStorage;

namespace CeskyRozhlasWP8.Model
{
    public class UserSettings
    {
        IsolatedStorageSettings settings = null;
        const string PovolitPrehravaniNaPozadiSettingKeyName = "PovolitPrehravaniNaPozadiSetting";

        const bool PovolitPrehravaniNaPozadiSettingDefault = false;
        const string PovolitNahravaniSettingKeyName = "PovolitNahravaniSetting";

        const bool PovolitNahravaniSettingDefault = false;


        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public UserSettings()
        {
            settings = DesignerProperties.IsInDesignTool ? null : IsolatedStorageSettings.ApplicationSettings;
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {

            if (settings == null) 
                return false;

            var valueChanged = false;
            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
                // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="iKey"></param>
        /// <param name="iDefaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string iKey, T iDefaultValue)
        {

            if (settings == null) 
                return iDefaultValue;

            // If the key exists, retrieve the value.
            T value;
            if (settings.Contains(iKey))
            {
                value = (T)settings[iKey];
            }
            else
            {
                value = iDefaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            if (settings != null)
            {
                settings.Save();
            }
        }



        /// <summary>
        /// Property to get and set a CheckBox Setting Key.
        /// </summary>
        public bool PovolitPrehravaniNaPozadiSetting
        {
            get
            {
                return GetValueOrDefault<bool>(PovolitPrehravaniNaPozadiSettingKeyName, PovolitPrehravaniNaPozadiSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(PovolitPrehravaniNaPozadiSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        public bool PovolitNahravaniSetting
        {
            get
            {
                return GetValueOrDefault<bool>(PovolitNahravaniSettingKeyName, PovolitNahravaniSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(PovolitNahravaniSettingKeyName, value))
                {
                    Save();
                }
            }
        }

    }
}
